﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using br.com.SistemaChamados;
using br.com.SistemaChamados.Controller;
using br.com.SistemaChamados.Model;
using br.com.SistemaChamados.Model.Common;
using br.com.SistemaChamados.Enum;
using br.com.SistemaChamados.Constants;
using br.com.SistemaChamados.Controller.Common;
using br.com.SistemaChamados.Util;

public partial class Pages_CallMain : System.Web.UI.Page
{

    private const string userLoggedIn = "userLoggedIn";

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        ViewStateUserKey = Session.SessionID;
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        br.com.SistemaChamados.Model.Common.User loggedUser = new br.com.SistemaChamados.Model.Common.User();
        UserController userController = new UserController();
        if (Session[UserConstants.SESSION_USER_LOGGED_IN] != null)
        {
            loggedUser.Id = Convert.ToInt32(Session[UserConstants.SESSION_USER_LOGGED_IN]);
            loggedUser = userController.GetUser(loggedUser);
            this.MasterPageFile = MasterPageDefiner.defineMasterPageByAccessRole(loggedUser.Profile);
            ((Label)Master.FindControl("lblUser")).Text = loggedUser.Name;
            this.Title = "SysCall - Sistema de chamados [logado como " + loggedUser.Name + "]";
        }
        else
        {
            Response.Redirect("~/Pages/Login/Login.aspx");
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillDdlStatus();
            FillDdlCategory();
            Search();
        }
    }

    private void FillDdlStatus()
    {
        CallStatusController callStatusController = new CallStatusController();
        ddlStatus.DataSource = callStatusController.GetCallStatus();
        ddlStatus.DataTextField = "Description";
        ddlStatus.DataValueField = "Id";
        ddlStatus.DataBind();
        ListItem item = new ListItem("Selecione", "-1");
        ddlStatus.Items.Insert(0, item);
    }

    private void FillDdlSubCategory()
    {
        CallSubCategoryController callSubCategoryController = new CallSubCategoryController();
        CallCategory callCategory = new CallCategory();
        CallSubCategory callSubCategory = new CallSubCategory();
        if (Convert.ToInt32(ddlCallCategory.SelectedIndex) != 0)
        {
            callCategory.Id = Convert.ToInt32(ddlCallCategory.SelectedValue);
            callSubCategory.CallCategory = callCategory;
            ddlCallSubCategory.DataSource = callSubCategoryController.GetCallSubCategoriesByCallCategory(callSubCategory);
            ddlCallSubCategory.DataTextField = "Description";
            ddlCallSubCategory.DataValueField = "Id";
            ddlCallSubCategory.DataBind();
            ListItem item = new ListItem("Selecione", "-1");
            ddlCallSubCategory.Items.Insert(0, item);
        }
        else
        {
            ClearDdlCallSubCategory();
        }
    }

    private void FillDdlCategory()
    {
        CallCategoryController callCategoryController = new CallCategoryController();
        ddlCallCategory.DataSource = callCategoryController.GetCallCategories();
        ddlCallCategory.DataTextField = "Description";
        ddlCallCategory.DataValueField = "Id";
        ddlCallCategory.DataBind();
        ListItem item = new ListItem("Selecione", "-1");
        ddlCallCategory.Items.Insert(0, item);
    }

    private void ClearDdlCallSubCategory()
    {
        if (ddlCallSubCategory.Items.Count > 0)
        {
            for (int i = 0; i < ddlCallSubCategory.Items.Count; i++)
            {
                ddlCallSubCategory.Items[i].Selected = false;
            }
            ddlCallSubCategory.DataSource = "";
            ddlCallSubCategory.DataBind();
        }
    }

    private void ClearDdlCallCategory()
    {
        if (ddlCallCategory.Items.Count > 0)
        {
            for (int i = 0; i < ddlCallCategory.Items.Count; i++)
            {
                ddlCallCategory.Items[i].Selected = false;
            }
            ddlCallCategory.Items[0].Selected = true;
            ddlCallCategory.DataBind();
        }
    }

    private void ClearDdlStatus()
    {
        if (ddlStatus.Items.Count > 0)
        {
            for (int i = 0; i < ddlStatus.Items.Count; i++)
            {
                ddlStatus.Items[i].Selected = false;
            }
            ddlStatus.Items[0].Selected = true;
            ddlStatus.DataBind();
        }
    }

    private void ClearFields()
    {
        txbId.Text = "";
        txbDataOpening.Text = "";
        txbProblemDescription.Text = "";
        ClearDdlCallCategory();
        ClearDdlCallSubCategory();
        ClearDdlStatus();
    }

    protected void ddlCallCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillDdlSubCategory();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Search();
    }

    private void Search()
    {
        List<Call> listCall = new List<Call>();
        CallController callController = new CallController();
        int? callId = null;
        //DateTime? dateOpening = null;
        string dateOpening = "";
        string problemDescription = "";
        int? categoryId = null;
        int? subCategoryId = null;
        int? statusId = null;
        try
        {
            string userId = Session[UserConstants.SESSION_USER_LOGGED_IN].ToString();
            User userOpen = new User();
            int userOpenId = Convert.ToInt32(userId);
            if (txbId.Text != "")
            {
                callId = Convert.ToInt32(txbId.Text);
            }
            if (txbDataOpening.Text != "")
            {
                dateOpening = Convert.ToDateTime(txbDataOpening.Text).ToString("d");
            }
            if (txbProblemDescription.Text != "")
            {
                problemDescription = Convert.ToString(txbProblemDescription.Text);
            }
            if (!ddlCallCategory.SelectedValue.Equals(string.Empty) && ddlCallCategory.SelectedIndex > 0)
            {
                categoryId = Convert.ToInt32(ddlCallCategory.SelectedValue);
            }
            if (!ddlCallSubCategory.SelectedValue.Equals(string.Empty) && ddlCallSubCategory.SelectedIndex > 0)
            {
                subCategoryId = Convert.ToInt32(ddlCallSubCategory.SelectedValue);
            }
            if (!ddlStatus.SelectedValue.Equals(string.Empty) && ddlStatus.SelectedIndex > 0)
            {
                statusId = Convert.ToInt32(ddlStatus.SelectedValue);
            }
            gvCall.DataSource = callController.GetCallByUser(userOpenId, callId, dateOpening, problemDescription, categoryId, subCategoryId, statusId);
            gvCall.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}